package cn.coderliu.voteweb.webcontroller;


import cn.coderliu.voteweb.common.util.R;
import cn.coderliu.voteweb.entity.Program;
import cn.coderliu.voteweb.service.ProgramService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liu
 */
@Controller
public class EntityController {

    @Autowired
    ProgramService programService;

    @GetMapping(value = "/entity")
    public String entity() {

        return "/entity";
    }

    @GetMapping(value = "/entityItem")
    public String entityItem(Long id) {

        return "/entityItem";
    }



    @ResponseBody
    @GetMapping(value = "/entityList")
    public Object getlist() {
        String sql = "from Program e where 1=1";
        List<Program> programs = programService.jpqlSearch(sql);
        Map<String, Object> m = new HashMap<>(16);
        m.put("statusCode", "200");
        m.put("totalPages", programs.size());
        m.put("statusMsg", "");
        m.put("data", programs);
        return m;
    }

    @ResponseBody
    @PostMapping(value = "/entityAddAndUpdate")
    public Object addAndUpdate(Program program) {
        programService.update(program);
        return R.ok();
    }

    @ResponseBody
    @GetMapping(value = "/entity/{id}")
    public Object addAndUpdate(@PathVariable Long id) {
        programService.batchUpdate("delete Program e where e.id=" + id);
        return R.ok();
    }


}
